﻿#light

module AstTypes

open Might.Infrastructure.ModelicaDOM
open Might.Infrastructure.ModelicaDOM.Expressions
open Might.Infrastructure.DOM
open System.Collections.ObjectModel
open System.Collections.Generic
open System
open Microsoft.FSharp.Text.Lexing

type ParsingException<'tok>(   currentTok:'tok, reduceTok: int list, shiftTok: int list, statStck: int list, msg:string ) =        
    inherit System.Exception() 
    member x.currenToken = currentTok
    member x.reduceTokens = reduceTok
    member x.shiftTokens = shiftTok
    member x.stateStack = statStck
    override x.Message = msg
   (*   
type Position() =
    let _startLine = 0
    let _startColumn = 0
    let _endLine = 0
    let _endColumn = 0
*)
 (***************************
 * Raw ast type, produced during ast creation in parser, later not used
 ***************************)


type ImportClause = class    
    val mutable _importedName:string
    val mutable _alias:string    
    new() = {
        _importedName = String.Empty
        _alias = String.Empty
    }
end

type SimpleEquation() = class
    [<DefaultValue>]
    val mutable _simpleExpLeft : SimpleExpression
    [<DefaultValue>]
    val mutable _expRight: Expression
end

and IfEquation() = class
    [<DefaultValue>]
    val mutable _condition : Expression
    [<DefaultValue>]
    val mutable _thenEq : Equation list
    [<DefaultValue>]
    val mutable _elseifList : (Expression * (Equation list)) list
    [<DefaultValue>]
    val mutable _elseEq : Equation list
end 

and ForEquation() = class
    [<DefaultValue>]
    val mutable _forIndices : ForIndices
    [<DefaultValue>]
    val mutable _equationList : Equation list        
end

and ConnectClause() = class
    [<DefaultValue>]
    val mutable _leftCompReference : ComponentReference
    [<DefaultValue>]
    val mutable _rightCompReference : ComponentReference
end

and WhenEquation() = class
    [<DefaultValue>]
    val mutable _condition : Expression
    [<DefaultValue>]
    val mutable _thenEq : Equation list
    [<DefaultValue>]
    val mutable _elseWhenList : (Expression * (Equation list)) list
end

and EquationOrAnnotation =
    | Eq of Equation
    | Annot of Annotation

and EquationSection() = class
    [<DefaultValue>]
    val mutable _initial : bool
    [<DefaultValue>]
    val mutable _eqList : EquationOrAnnotation list
end

and Equation =
    | SimpleEq of SimpleEquation * Comment
    | IfEq of IfEquation * Comment
    | ForEq of ForEquation * Comment
    | ConnClause of ConnectClause * Comment
    | WhenEq of WhenEquation * Comment
    | EqFncCall of FunctionCall * Comment

and ExtendClause = class
    val mutable _baseClassName:string   
    val mutable _baseClass: Class option 
    val mutable _modification: ClassModification option
    val mutable _domObject: IDomNode option
    val mutable _annotation: Annotation option
    new() = {    
        _baseClassName = String.Empty
        _baseClass = None
        _modification = None
        _annotation = None
        _domObject = None
    }
end

and TypePrefix = class
    val mutable _flow: bool
    val mutable _discrete: bool    
    val mutable _parameter: bool      
    val mutable _constant: bool    
    val mutable _input: bool
    val mutable _output: bool
    new() = {
        _flow = false
        _discrete = false 
        _parameter = false
        _constant = false 
        _input = false
        _output = false        
    }  
end

and Modification = class
    val mutable _classModification:ClassModification option
    val mutable _assignedValue: Expression option
    new(clsModif,assignedVal) = {
        _classModification = clsModif
        _assignedValue = assignedVal
    }
end    
 
/// Used for tracking information about defined components an their modifications
and Declaration = class
    val mutable _name:string
    val mutable _modification: Modification
    val mutable _comment:Comment
    new(name,modification) = {
        _name = name
        _modification = modification
        _comment = new Comment()
    }    
end

and Redefinition = class
    val mutable _each: bool
    val mutable _final: bool
    val mutable _redeclare: bool
    val mutable _replaceable: bool
    new() = {
        _each = false
        _final = false
        _redeclare = false
        _replaceable = false
    }    
end

and ClassRedefinition = class        
    inherit Redefinition
    val mutable _class: Class
    new() = {
        _class = new Class()
    }
end
and ComponentRedefinition = class
    inherit Redefinition       
    val mutable _component: Component
    new() = {
        _component = Component()
    }
end
and ComponentModification = class
    val mutable _compReference:ComponentReference
    val mutable _modification: ClassModification option
    val mutable _comment: string
    val mutable _assignedValue: Expression option 
    val mutable _each:bool    
    val mutable _final:bool
    new() = {
        _compReference = new ComponentReference([])
        _modification = None
        _comment = String.Empty
        _assignedValue = None
        _each = false
        _final = false
    }
end
and ClassModification = class
    val mutable _compRedef: Dictionary<string,ComponentRedefinition>
    val mutable _classRedef: Dictionary<string,ClassRedefinition>
    val mutable _compModif: Dictionary<string,ComponentModification>
    new() = {
        _compRedef = new Dictionary<string,ComponentRedefinition>()
        _classRedef = new Dictionary<string,ClassRedefinition>()
        _compModif = new Dictionary<string,ComponentModification>()
    }
end

and ModificatedType = class
    val mutable _type: Class
    val mutable _modification: ClassModification option
    new() = {
        _type = new Class()
        _modification = None
    }
end
   
and Class = class    
    
    val mutable _name:string
    val mutable _qualifiedName:string
        
    val _publicInheritsFrom:Dictionary<string,ExtendClause>
    val _protectedInheritsFrom:Dictionary<string,ExtendClause>
    val _isBaseFor:Dictionary<string,Class>
    
    val mutable _initialEquations : Equation list
    val mutable _equations : Equation list
    val mutable _initialEquationsAnnotations: Annotation list
    val mutable _equationsAnnotations: Annotation list
    
    val mutable _annotations: ResizeArray<Annotation>
    val mutable _enclosingClass: Class option
    val mutable _components:Dictionary<string,Component>    
    val mutable _nestedClasses:Dictionary<string,Class>  
       
    val mutable _publicImportedNames: ImportClause list    
    val mutable _protectedImportedNames: ImportClause list
       
    val mutable _visibility: Visibility        
    val mutable _final:bool
    val mutable _encapsulated:bool
    val mutable _partial:bool
    val mutable _replaceable:bool
    val mutable _redeclare:bool
    val mutable _inner:bool
    val mutable _outer:bool
    val mutable _expandable:bool
    val mutable _restrictedType: ModelicaClassType
    
    val mutable _domObject: IDomNode option
    
    [<DefaultValue>]  
    val mutable _startPosition: Position
    
    [<DefaultValue>]  
    val mutable _endPosition: Position
    
    new() = {
    
        _name = String.Empty
        _qualifiedName = String.Empty
        
        _publicInheritsFrom  = new Dictionary<string,ExtendClause>()
        _protectedInheritsFrom  = new Dictionary<string,ExtendClause>()
        _isBaseFor = new Dictionary<string,Class>()
    
        _initialEquations = []
        _equations = []
        _initialEquationsAnnotations = []
        _equationsAnnotations = []
    
        _annotations = new ResizeArray<Annotation>()
        _enclosingClass = None
        _components = new Dictionary<string,Component>()
        _nestedClasses = new Dictionary<string,Class>()
        _publicImportedNames = []
        _protectedImportedNames = []
        _visibility = Visibility.Public        
        _final = false
        _encapsulated = false
        _partial = false
        _replaceable = false
        _redeclare = false
        _inner = false
        _outer = false
        _expandable = false
        _restrictedType = ModelicaClassType.Class
        _domObject = None
        }    
end
and  Component = class
    val mutable _name:string
    val mutable _type:Class
    val mutable _modification:ClassModification option
    val mutable _assignedValue:Expression option
    
    val mutable _enclosingClass:Class
    val mutable _visibility: Visibility        
    val mutable _redeclare:bool
    val mutable _final:bool
    val mutable _inner:bool
    val mutable _outer:bool
    val mutable _replaceable:bool    
    val mutable _flow:bool
    val mutable _discrete:bool
    val mutable _parameter:bool
    val mutable _constant:bool
    val mutable _input:bool
    val mutable _output:bool
    val mutable _constrainType: ModificatedType option
    val mutable _comment: Comment
    val mutable _domObject: IDomNode option
    new() = {
        _name = String.Empty
        _type = new Class()
        _modification = None
        _assignedValue = None
        _enclosingClass = new Class()
        _visibility = Visibility.Public        
        _redeclare = false
        _final = false
        _inner = false
        _outer = false
        _replaceable = false
        _flow = false
        _discrete = false
        _parameter = false
        _constant = false
        _input = false
        _output = false       
        _constrainType = None
        _comment = new Comment()
        _domObject = None
    }       
end
and Annotation = class
    val mutable _annotationBody: ClassModification
    new() = {
            _annotationBody = new ClassModification()
        }
end

and Comment = class
    val mutable _annotation: Annotation option
    val mutable _strComment: string
    new() = {
            _annotation = None
            _strComment = String.Empty
        }
end

// ast raw type
type Element = 
    | ComponentDef of Component
    | ClassDef of Class   
    | ImportDef of ImportClause
    | ExtendDef of ExtendClause
    | AnnotationDef of Annotation

type ElementList = class
    val mutable _visibility:Visibility
    val mutable _elementList: Element list
    new() = {
        _visibility = Visibility.Public
        _elementList = []
    }
end

type ModificationElement =
    | ClassModificationDef of ClassModification
    | ClassRedefinitionDef of ClassRedefinition
    | ComponentModificationDef of ComponentModification
    | ComponentRedefinitionDef of ComponentRedefinition
    
type Algorithm = class
    new() = {}
end    

type External = class
    new() = {}
end    

type Section = 
    | ElementSection of ElementList
    | EqSection of EquationSection
    | AlgorithmSection of Algorithm
    | ExternalSection of External

type Composition = 
    Section list


    